package com.surekam.platform.core.security;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PermissionsFilter implements Filter {

	private static Logger logger = LoggerFactory.getLogger(PermissionsFilter.class.getName());

	private Map<String, String> map = new HashMap<String, String>();

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		logger.info("Permissions Filter init......");
		map.put("01", "/auth/authuser");
		map.put("01", "/locate/getlocation");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		
		String uri = req.getRequestURI();
		String pathInfo = req.getPathInfo();

		int i = uri.indexOf(pathInfo);
		if(i > 0){
			logger.info("没有权限");
		}
		logger.info("URL PATH:" + urlpath(req));
		logger.info("访问URL：" + uri);
		logger.info("PathInfo:" + pathInfo);
		
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {

	}

	private String urlpath(HttpServletRequest req){
		if(req == null){
			return null;
		}
		String host = req.getHeader("host");
		String uri = req.getRequestURI();
		
		return "http://" + host + uri;
	}
	
}
